import {addOrg, removeOrg, queryOrgList, queryTreeOrgList, updateOrg, adminOrg} from '@/services/system/org';

export default {
  namespace: 'org',

  state: {
    // 树状列表
    treeList: [],

    //选中列表数据
    data: {
      dataList: [],
      pageNum: 0,
      pageSize: 0,
      totalCount: 0,
    },

    //查询条件
    queryForm: {},
    adminData: [], //组织管理员

  },
  // effects： 处理所有的异步逻辑，将返回结果以Action的形式交给reducer处理；
  effects: {
    * getTree({payload}, {call, put}) {
      const response = yield call(queryTreeOrgList, payload);
      yield put({
        type: 'saveTree',
        payload: response,
      });
    },
    * getList({payload, callback}, {call, put}) {
      const response = yield call(queryOrgList, payload);
      if(response&&response.result=='ok'){
        yield put({
          type: 'saveList',
          payload: response,
        });
      }

      if (callback) callback(response);
    },
    * add({payload, callback}, {call, put}) {
      const response = yield call(addOrg, payload);

      yield put({
        type: 'saveadd',
        payload: response,
      });
      if (callback) callback(response);
    },
    * remove({payload, callback}, {call, put}) {
      const response = yield call(removeOrg, payload);
      yield put({
        type: 'saveremove',
        payload: response,
      });
      if (callback) callback(response);
    },
    * update({payload, callback}, {call, put}) {
      const response = yield call(updateOrg, payload);
      yield put({
        type: 'saveupdate',
        payload: response,
      });
      if (callback) callback(response);
    },
    * admin({payload, callback}, {call, put}) {
      const response = yield call(adminOrg, payload);
      yield put({
        type: 'adminData',
        payload: response,
      });
      if (callback) callback(response);
    },
  },
  // reducers：处理所有的同步逻辑，将数据返回给页面；
  reducers: {
    saveTree(state, action) {
      return {
        ...state,
        treeList: action.payload.data,
      };
    },
    saveList(state, action) {
      return {
        ...state,
        data: action.payload.data,
      };
    },
    adminData(state, action) {
      return {
        ...state,
        adminData: action.payload.data,
      };
    },

  },
};
